package com.shangdiao.sd_server.server.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.shangdiao.sd_server.constant.Constant;
import com.shangdiao.sd_server.constant.ErrorCode;
import com.shangdiao.sd_server.generator.pojo.SdDicCertificate;
import com.shangdiao.sd_server.generator.pojo.SdDicCertificateExample;
import com.shangdiao.sd_server.generator.pojo.SdDicCompanyField;
import com.shangdiao.sd_server.generator.pojo.SdDicCompanyFieldExample;
import com.shangdiao.sd_server.generator.pojo.SdDicDistrict;
import com.shangdiao.sd_server.generator.pojo.SdDicDistrictExample;
import com.shangdiao.sd_server.generator.pojo.SdDicEducation;
import com.shangdiao.sd_server.generator.pojo.SdDicEducationExample;
import com.shangdiao.sd_server.generator.pojo.SdDicInternshipField;
import com.shangdiao.sd_server.generator.pojo.SdDicInternshipFieldExample;
import com.shangdiao.sd_server.generator.pojo.SdDicParttimeField;
import com.shangdiao.sd_server.generator.pojo.SdDicParttimeFieldExample;
import com.shangdiao.sd_server.generator.pojo.SdDicSettlement;
import com.shangdiao.sd_server.generator.pojo.SdDicSettlementExample;
import com.shangdiao.sd_server.generator.pojo.SdDicUniversity;
import com.shangdiao.sd_server.generator.pojo.SdDicUniversityExample;
import com.shangdiao.sd_server.pojo.ResponseMsg;
import com.shangdiao.sd_server.pojo.Row;
import com.shangdiao.sd_server.service.DicService;
import com.shangdiao.sd_server.util.RequestUtil;
import com.shangdiao.sd_server.util.StringUtil;


@Controller
@RequestMapping( "/open/dic" )
public class DicController {

	@Autowired
	DicService dicService;

	
	
	
	@RequestMapping(value = { "/selectCompanyField.json" })
	@ResponseBody
	@Transactional
	public ResponseMsg selectCompanyField(HttpServletRequest request, HttpServletResponse response) {
		ResponseMsg retMsg = null;
		SdDicCompanyFieldExample example = new SdDicCompanyFieldExample();
		List<SdDicCompanyField> company_field_list = dicService.selectByExample(example);
		retMsg = new ResponseMsg("查询成功", ErrorCode.OK, company_field_list);
		return retMsg;
	}
	
	
	@RequestMapping(value = { "/selectEducation.json" })
	@ResponseBody
	@Transactional
	public ResponseMsg selectEducation(HttpServletRequest request, HttpServletResponse response) {
		ResponseMsg retMsg = null;
		SdDicEducationExample example = new SdDicEducationExample();
		List<SdDicEducation> education_list = dicService.selectByExample(example);
		retMsg = new ResponseMsg("查询成功", ErrorCode.OK, education_list);
		return retMsg;
	}
	
	@RequestMapping(value = { "/selectSettlement.json" })
	@ResponseBody
	@Transactional
	public ResponseMsg selectSettlement(HttpServletRequest request, HttpServletResponse response) {
		ResponseMsg retMsg = null;
		SdDicSettlementExample example = new SdDicSettlementExample();
		List<SdDicSettlement> settlement_list = dicService.selectByExample(example);
		retMsg = new ResponseMsg("查询成功", ErrorCode.OK, settlement_list);
		return retMsg;
	}
	
	@RequestMapping(value = { "/selectParttimeField.json" })
	@ResponseBody
	@Transactional
	public ResponseMsg selectParttimeField(HttpServletRequest request, HttpServletResponse response) {
		ResponseMsg retMsg = null;
		SdDicParttimeFieldExample example = new SdDicParttimeFieldExample();
		List<SdDicParttimeField> field_list = dicService.selectByExample(example);
		retMsg = new ResponseMsg("查询成功", ErrorCode.OK, field_list);
		return retMsg;
	}
	
	@RequestMapping(value = { "/selectInternshipField.json" })
	@ResponseBody
	@Transactional
	public ResponseMsg selectInternshipField(HttpServletRequest request, HttpServletResponse response) {
		ResponseMsg retMsg = null;
		List<String> category_list = dicService.selectInternshipCategory();
		List<Row> resultList = new ArrayList<Row>();
		for(String category : category_list){
			Row result = new Row();
			SdDicInternshipFieldExample example = new SdDicInternshipFieldExample();
			example.or().andCategoryEqualTo(category).andDelEqualTo(Constant.DEL_NO);
			List<SdDicInternshipField> field_list = dicService.selectByExample(example);
			result.put("categoryName", category);
			result.put("categoryDetail", field_list);
			resultList.add(result);
		}
		retMsg = new ResponseMsg("查询成功", ErrorCode.OK, resultList);
		return retMsg;
	}
	
	@RequestMapping(value = { "/selectInternshipField4Page.json" })
	@ResponseBody
	@Transactional
	public ResponseMsg selectInternshipField4Page(HttpServletRequest request, HttpServletResponse response) {
		ResponseMsg retMsg = null;
		SdDicInternshipFieldExample example = new SdDicInternshipFieldExample();
		example.or().andDelEqualTo(Constant.DEL_NO);
		List<SdDicInternshipField> field_list = dicService.selectByExample(example);
		List<Row> resultList = new ArrayList<Row>();
		for(SdDicInternshipField field : field_list){
			Row result = new Row();
			result.put("dicFieldId", field.getDicFieldId());
			result.put("dicFieldName", field.getCategory() + "-" + field.getDicFieldName());
			resultList.add(result);
		}
		retMsg = new ResponseMsg("查询成功", ErrorCode.OK, resultList);
		return retMsg;
	}
	
	@RequestMapping(value = { "/searchUniversity.json" })
	@ResponseBody
	@Transactional
	public ResponseMsg searchUniversity(HttpServletRequest request, HttpServletResponse response) {
		ResponseMsg retMsg = null;
		String university = RequestUtil.get(request, "university");
		Integer dicDistrictId = RequestUtil.getInt(request, "dicDistrictId", 0);
		SdDicUniversityExample example = new SdDicUniversityExample();
		if(dicDistrictId != 0 && !StringUtil.isBlank(university)){
			example.or()
				.andDicUniversityNameLike("%" + university + "%")
				.andDicDistrictIdEqualTo(dicDistrictId)
				.andDelEqualTo(Constant.DEL_NO);
		}else if(dicDistrictId == 0 && !StringUtil.isBlank(university)){
			example.or()
			.andDicUniversityNameLike("%" + university + "%")
			.andDelEqualTo(Constant.DEL_NO);
		}else if(dicDistrictId != 0 && StringUtil.isBlank(university)){
			example.or()
			.andDicDistrictIdEqualTo(dicDistrictId)
			.andDelEqualTo(Constant.DEL_NO);
		}else if(dicDistrictId == 0 && StringUtil.isBlank(university)){
			example.or()
			.andDelEqualTo(Constant.DEL_NO);
		}
		List<SdDicUniversity> universict_list = dicService.selectByExample(example);
		retMsg = new ResponseMsg("查询成功", ErrorCode.OK, universict_list);
		return retMsg;
	}
	
	@RequestMapping(value = { "/selectUniversity.json" })
	@ResponseBody
	@Transactional
	public ResponseMsg selectUniversity(HttpServletRequest request, HttpServletResponse response) {
		SdDicDistrictExample example1 = new SdDicDistrictExample();
		example1.or().andLevelEqualTo(Constant.DISTRICT_PROVINCE).andDelEqualTo(Constant.DEL_NO).andParentIdIsNull();
		List<SdDicDistrict> provinceList = dicService.selectByExample(example1);
		List<Row> resultList = new ArrayList<Row>();
		for(SdDicDistrict province : provinceList){
			Row result = new Row();
			Integer dicDistrictId = province.getDicDistrictId();
			result.put("dicDistrictId", dicDistrictId);
			result.put("dicDistrict", province.getDicDistrict());
			SdDicUniversityExample example2 = new SdDicUniversityExample();
			example2.or().andDicDistrictIdEqualTo(dicDistrictId).andDelEqualTo(Constant.DEL_NO);
			List<SdDicUniversity> universityList = dicService.selectByExample(example2);
			List<Row> universityRowList = new ArrayList<Row>();
			for(SdDicUniversity university : universityList){
				Row universityRow = new Row();
				universityRow.put("dicUniversityId", university.getDicUniversityId());
				universityRow.put("dicUniversityName", university.getDicUniversityName());
				universityRowList.add(universityRow);
			}
			result.put("universityList", universityRowList);
			resultList.add(result);
		}
		return new ResponseMsg("查询成功", ErrorCode.OK, resultList);
	}
	
	@RequestMapping(value = { "/selectDistrictList.json" })
	@ResponseBody
	@Transactional
	public ResponseMsg selectDistrictList(HttpServletRequest request, HttpServletResponse response) {
		ResponseMsg retMsg = null;
		Integer level = RequestUtil.getInt(request, "level");
		Integer parentId = RequestUtil.getInt(request, "parentId");
		SdDicDistrictExample example = new SdDicDistrictExample();
		if(parentId != null){
			example.or().andLevelEqualTo(level).andParentIdEqualTo(parentId).andDelEqualTo(Constant.DEL_NO);
		}else{
			example.or().andLevelEqualTo(level).andDelEqualTo(Constant.DEL_NO);
		}
		List<SdDicDistrict> districtList = dicService.selectByExample(example);
		retMsg = new ResponseMsg("查询成功", ErrorCode.OK, districtList);
		return retMsg;
	}
	
	@RequestMapping(value = { "/selectDistrict.json" })
	@ResponseBody
	@Transactional
	public ResponseMsg selectDistrict(HttpServletRequest request, HttpServletResponse response) {
		ResponseMsg retMsg = null;
		Integer districtId = RequestUtil.getInt(request, "districtId");
		SdDicDistrict district = dicService.selectDistrictById(districtId);
		retMsg = new ResponseMsg("查询成功", ErrorCode.OK, district);
		return retMsg;
	}
	
	@RequestMapping(value = { "/getDistrict.json" })
	@ResponseBody
	@Transactional
	public ResponseMsg getDistrict(HttpServletRequest request, HttpServletResponse response) {
		ResponseMsg retMsg = null;
		List<Row> provinceList = new ArrayList<Row>();
		SdDicDistrictExample example1 = new SdDicDistrictExample();
		example1.or().andLevelEqualTo(Constant.DISTRICT_PROVINCE).andParentIdIsNull().andDelEqualTo(Constant.DEL_NO);
		List<SdDicDistrict> list1 = dicService.selectByExample(example1);
		for(SdDicDistrict temp1 : list1){
			Row province = new Row();
			Integer dicDistrictId = temp1.getDicDistrictId();
			province.put("dicDistrictId", dicDistrictId);
			province.put("dicDistrict", temp1.getDicDistrict());
			List<Row> cityList = new ArrayList<Row>();
			SdDicDistrictExample example2 = new SdDicDistrictExample();
			example2.or().andLevelEqualTo(Constant.DISTRICT_CITY).andParentIdEqualTo(dicDistrictId).andDelEqualTo(Constant.DEL_NO);
			List<SdDicDistrict> list2 = dicService.selectByExample(example2);
			for(SdDicDistrict temp2 : list2){
				Row city = new Row();
				Integer cityId = temp2.getDicDistrictId();
				city.put("dicDistrictId", cityId);
				city.put("dicDistrict", temp2.getDicDistrict());
				List<Row> areaList = new ArrayList<Row>();
				SdDicDistrictExample example3 = new SdDicDistrictExample();
				example3.or().andLevelEqualTo(Constant.DISTRICT_AREA).andParentIdEqualTo(cityId).andDelEqualTo(Constant.DEL_NO);
				List<SdDicDistrict> list3 = dicService.selectByExample(example3);
				for(SdDicDistrict temp3 : list3){
					Row area = new Row();
					area.put("dicDistrictId", temp3.getDicDistrictId());
					area.put("dicDistrict", temp3.getDicDistrict());
					areaList.add(area);
				}
				city.put("districtList", areaList);
				cityList.add(city);
			}
			province.put("districtList", cityList);
			provinceList.add(province);
		}
		retMsg = new ResponseMsg("查询成功", ErrorCode.OK, provinceList);
		return retMsg;
	}
	
	@RequestMapping(value = { "/selectCertificate.json" })
	@ResponseBody
	@Transactional
	public ResponseMsg selectCertificate(HttpServletRequest request, HttpServletResponse response) {
		ResponseMsg retMsg = null;
		List<String> category_list = dicService.selectCertificateCategory();
		List<Row> resultList = new ArrayList<Row>();
		for(String category : category_list){
			Row result = new Row();
			SdDicCertificateExample example = new SdDicCertificateExample();
			example.or().andCategoryEqualTo(category).andDelEqualTo(Constant.DEL_NO);
			List<SdDicCertificate> certificate_list = dicService.selectByExample(example);
			result.put("categoryName", category);
			result.put("categoryDetail", certificate_list);
			resultList.add(result);
		}
		retMsg = new ResponseMsg("查询成功", ErrorCode.OK, resultList);
		return retMsg;
	}
 	 
}
